<?php
/**
 * @Created on 2020/01/15
 * @Author QQ 1026269609@qq.com
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Bumen extends Base
{
    public function index(){
    	$info=db('bumen')->order('id desc')->select();
    	$this->assign('info',$info);
        return $this->fetch();
    }
    public function addbumen(){
    	if(request()->isPost()){
			$title=request()->param('title');
			$data=['title'=>$title];
			$info=db('bumen')->where('title',$title)->find();
			if($info){
				return json(['status'=>0,'message'=>'名称已存在']);
			}
			$true=db('bumen')->insert($data);
			if($true){
				return json(['status'=>1,'message'=>'添加成功']);
			}else{
				return json(['status'=>0,'message'=>'添加失败']);
			}
    	}else{
			return $this->fetch();
    	}
    }
    public function editbumen(){
    	if(request()->isPost()){
    		$id=request()->param('id');
			$title=request()->param('title');
			$data=['title'=>$title];
			$info=db('bumen')->where('id','neq',$id)->where('title',$title)->find();
			if($info){
				return json(['status'=>0,'message'=>'名称已存在']);
			}
			$true=db('bumen')->where('id',$id)->update($data);
			if($true){
				return json(['status'=>1,'message'=>'修改成功']);
			}else{
				return json(['status'=>0,'message'=>'修改失败']);
			}
    	}else{
    		$id=request()->param('id');
			$info=db('bumen')->where('id',$id)->find();
			$this->assign('info',$info);
			return $this->fetch();
    	}
    }
    public function delbumen(){
    	$id=request()->param('id');
		$info=db('member')->where('bumenid',$id)->find();
		if($info){
			return json(['status'=>0,'message'=>'该部门下存在用户，无法删除']);
		}
		$true=db('bumen')->where('id',$id)->delete();
		if($true){
			return json(['status'=>1,'message'=>'删除成功']);
		}else{
			return json(['status'=>0,'message'=>'删除失败']);
		}
    }
}